﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using DNA.Mvc.Models;
using System.Web.UI;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace DNA.Mvc.UI
{
    public class Widget : ViewComponent
    {
        private UrlHelper urlHelper;
        private HtmlHelper helper;

        internal HtmlHelper Html
        {
            get { return helper; }
            set { helper = value; }
        }

        private UrlHelper Url
        {
            get
            {
                if (urlHelper == null)
                    urlHelper = UrlUtility.CreateUrlHelper();
                return urlHelper;
            }
        }

        public WidgetInstance Model { get; set; }

        public bool IsDesignModel
        {
            get
            {
                return true;
            }
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            if (!this.Model.ShowBorder)
                this.MergeAttribute("class", "d-widget d-widget-no-border");
            else
                this.MergeAttribute("class", "d-widget");

            if ((Model != null) && (!string.IsNullOrEmpty(Model.CssText)))
                MergeAttribute("style", Model.CssText);

            base.RenderBeginTag(writer);
        }

        public override void RenderContent(HtmlTextWriter writer)
        {
            if (Model.ShowHeader)
            {
                writer.WriteBeginTag("h2");
                writer.WriteAttribute("class", "d-widget-header");

                if (!string.IsNullOrEmpty(Model.HeaderCssText))
                    writer.WriteAttribute("style", Model.HeaderCssText);

                writer.Write(HtmlTextWriter.TagRightChar);

                if (!string.IsNullOrEmpty(Model.TitleLinkUrl))
                {
                    writer.WriteBeginTag("a");
                    writer.WriteAttribute("class", "d-widget-title-link");
                    if (!string.IsNullOrEmpty(Model.TitleLinkUrl))
                        writer.WriteAttribute("href", Url.Content(Model.TitleLinkUrl));
                    else
                        writer.WriteAttribute("href", "javascript:void(0);");

                    writer.Write(HtmlTextWriter.TagRightChar);
                }

                if (!string.IsNullOrEmpty(Model.IconUrl))
                {
                    writer.WriteBeginTag("img");
                    writer.WriteAttribute("class", "d-widget-icon");
                    writer.WriteAttribute("src", Url.Content(Model.IconUrl));
                    writer.WriteAttribute("alt", Model.Title);
                    writer.Write(HtmlTextWriter.SelfClosingTagEnd);
                    //writer.WriteEndTag("img");
                }

                writer.WriteBeginTag("span");
                writer.WriteAttribute("class", "d-widget-title-text");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Write(Model.Title);
                writer.WriteEndTag("span");

                if (!string.IsNullOrEmpty(Model.TitleLinkUrl))
                {
                    writer.WriteEndTag("a");
                }
                writer.WriteEndTag("h2");
            }

            writer.WriteBeginTag("div");
            writer.WriteAttribute("class", "d-widget-body");

            if (!string.IsNullOrEmpty(Model.BodyCssText))
                writer.WriteAttribute("style", Model.BodyCssText);

            writer.Write(HtmlTextWriter.TagRightChar);

            try
            {

                if (!string.IsNullOrEmpty(Model.Controller) && !string.IsNullOrEmpty(Model.Action))
                {
                    if (string.IsNullOrEmpty(Model.Area))
                        Html.RenderAction(Model.Action, Model.Controller, new { Area = "", wid = Model.ID.ToString(), preview = false, design = false });
                    else
                        Html.RenderAction(Model.Action, Model.Controller, new { Area = Model.Area, wid = Model.ID.ToString(), preview = false, design = false });
                }
                else
                {
                    Html.RenderAction("Generic", "Widget", new { Area = "", wid = Model.ID.ToString(), preview = false, design = false });
                }
            }
            catch (Exception e)
            {
                writer.WriteBeginTag("div");
                writer.WriteAttribute("class", "ui-state-error");
                writer.WriteAttribute("style", "white-space:normal;padding:10px;");
                writer.Write(Html32TextWriter.TagRightChar);

                if (e.InnerException != null)
                    writer.Write(e.InnerException.Message);
                else
                    writer.Write(e.Message);
                writer.WriteEndTag("div");
                writer.WriteEndTag("div");
                writer.WriteEndTag("div");
            }

            writer.WriteEndTag("div");

        }
    }
}